//
//  MoviesPager.m
//  ShowTimeApp
//
//  Created by Ruby on 10/22/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "MoviesPager.h"
#import "CustomMovieInfoView.h"
#import "TrailerInfo.h"
#import "ShowTimeInfo.h"
#import "MyCustomButton.h"
#define BASEHEIGHT	380.0f
//#define NPAGES		20 // works better with 5 or more. will work with 3 minimum
#define BIGNUM		500

@implementation MoviesPager
@synthesize movieList;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title=@"Galaxy";
    }
    return self;
}

-(id)initWithMovieList:(NSMutableArray *)_movieList andCurrentIndex:(int)index andCinema:(id)_cinema{
    if(self){
        cinema=_cinema;
        currentPage=index;
        movieList=_movieList;
        NPAGES=movieList.count;
        self.title=cinema.Name;
    }
    return self;
}

//Update lai trang hien hanh
-(void)updatePagePlacement1{
    CGPoint offset = sv.contentOffset;
    // Current Page
    CustomMovieInfoView *movieInfo = (CustomMovieInfoView*)[sv viewWithTag:900+cPage];
    CGRect newframe = CGRectMake(offset.x, 0.0f, 320.0f, BASEHEIGHT);
    if (!CGRectEqualToRect(newframe, movieInfo.frame)) movieInfo.frame = newframe;
    
    // Pages to the left
	int half = (NPAGES / 2);
	float dx = -320.0f * half;
	for (int i = 0; i < half; i++)
	{
		int tag = (cPage + i + NPAGES - half) % NPAGES;
		// printf("Left: %d\n", tag);
        CustomMovieInfoView *movieInfo = (CustomMovieInfoView*)[sv viewWithTag:0+tag];
		CGRect newframe = CGRectMake(offset.x + dx, 0.0f, 320.0f, BASEHEIGHT);
		if (!CGRectEqualToRect(newframe, movieInfo.frame)) movieInfo.frame = newframe;
		dx += 320.0f;
	}
	
	// Pages to the right
	int nleft = NPAGES - half;
	dx = 320.0f;
	for (int i = 1; i < nleft; i++)
	{
		int tag = (cPage + i + NPAGES) % NPAGES;
		// printf("Right: %d\n", tag);
		CustomMovieInfoView *movieInfo = (CustomMovieInfoView*)[sv viewWithTag:0+tag];
		CGRect newframe = CGRectMake(offset.x + dx, 0.0f, 320.0f, BASEHEIGHT);
		if (!CGRectEqualToRect(newframe, movieInfo.frame)) movieInfo.frame = newframe;
		dx += 320.0f;
	}
}

//Su kien khi nguoi dung keo page

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    CGPoint offset =  sv.contentOffset;
	int thispage = (int)(offset.x / 320.0f) % NPAGES;
	[self updatePagePlacement];
	cPage = thispage;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGPoint offset = sv.contentOffset;
	cPage = (int)(offset.x / 320.0f) % NPAGES;
	pageControl.currentPage = cPage;
}

-(void)viewDidLoad{
    // Create the scroll view and set its content size and delegate
	sv = [[[UIScrollView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, BASEHEIGHT)] autorelease];
	sv.contentSize = CGSizeMake(BIGNUM * 2 * NPAGES * 320.0f, sv.frame.size.height);
	sv.contentOffset = CGPointMake(320.0f * BIGNUM * NPAGES, 0.0f);
	sv.pagingEnabled = YES;
	sv.showsHorizontalScrollIndicator = NO;
	sv.delegate = self;
    
    
	// Load list of movies into pages
	for (int i = 0; i < NPAGES; i++)
	{
        CustomMovieInfoView* movieInfo=[[CustomMovieInfoView alloc]initWithMovieInfo:(Movie*)[movieList objectAtIndex:i] andFrame:CGRectMake(0.0f, 0.0f, 320.0f, BASEHEIGHT)];
        movieInfo.tag=900+i;
        currentTag=900+i;
        
        MyCustomButton *viewShowtimeCmd = [[MyCustomButton alloc]init];
        [viewShowtimeCmd setBackgroundImage:[UIImage imageNamed:@"buttonBG_1.png"] forState:UIControlStateNormal];
        [viewShowtimeCmd setBackgroundImage:[UIImage imageNamed:@"buttonBG_1_touchUp.png"] forState:UIControlEventTouchUpInside];
        viewShowtimeCmd.argument = (Movie*)[movieList objectAtIndex:i];
        viewShowtimeCmd.tag=1000+i;
        [viewShowtimeCmd setFrame:CGRectMake(14.0f, 220.0f, 112.0f, 30.0f)];
        viewShowtimeCmd.titleLabel.textAlignment = UITextAlignmentRight;
        //[viewShowtimeCmd sizeToFit];
        viewShowtimeCmd.titleLabel.font=[UIFont systemFontOfSize:12];
        [viewShowtimeCmd setTitle:@"Showtime" forState:UIControlStateNormal];
        [viewShowtimeCmd addTarget:self action:@selector(viewShowTime:) forControlEvents:UIControlEventTouchUpInside];
        [movieInfo addSubview:viewShowtimeCmd];
        
        MyCustomButton *viewTrailerCmd = [[MyCustomButton alloc] init];
        [viewTrailerCmd setBackgroundImage:[UIImage imageNamed:@"buttonBG_1.png"] forState:UIControlStateNormal];
        [viewTrailerCmd setBackgroundImage:[UIImage imageNamed:@"buttonBG_1_touchUp.png"] forState:UIControlEventTouchUpInside];
        viewTrailerCmd.tag=2000+i;
        viewTrailerCmd.titleLabel.font=[UIFont systemFontOfSize:12];
        viewTrailerCmd.argument = (Movie*)[movieList objectAtIndex:i];
        [viewTrailerCmd setFrame:CGRectMake(14.0f, 259.0f, 112.0f, 30.0f)];
        //[viewTrailerCmd sizeToFit];
        [viewTrailerCmd setTitle:@"Trailer" forState:UIControlStateNormal];
        [viewTrailerCmd addTarget:self action:@selector(viewTrailer:) forControlEvents:UIControlEventTouchUpInside];
        [movieInfo addSubview:viewTrailerCmd];
             
        
        //        UIView *uv =[[UIView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 320, BASEHEIGHT)];
        //        [uv setBackgroundColor:[[UIColor alloc]initWithRed:1.0f green:1.0f blue:0.0f alpha:1.0f]];
        //        uv.tag=900+i;
        
        //        NSString *filename = [NSString stringWithFormat:@"image%d.png", ((i % 3) +1)];
        //        UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:filename]];
        //        iv.frame = CGRectMake(0.0f, 0.0f, 128.0f, BASEHEIGHT);
        //        iv.tag = 900 + i;
        
        //        UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
        //        label.text = [NSString stringWithFormat:@"Slide %d\n", i + 1];
        //        label.backgroundColor = [UIColor blackColor];
        //        label.textColor = [UIColor blackColor];
        //        [label sizeToFit];
        //        label.center = uv.center;
        //        [uv addSubview:label];
        //[uv addSubview:iv];
        
        [sv addSubview:movieInfo];
	}
    
    // Load in all the pages
    //    for (int i = 0; i < NPAGES; i++)
    //	{
    //		NSString *filename = [NSString stringWithFormat:@"image%d.png", ((i % 3) +1)];
    //		UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:filename]];
    //		iv.frame = CGRectMake(0.0f, 0.0f, 320.0f, BASEHEIGHT);
    //		iv.tag = 900 + i;
    //		[sv addSubview:iv];
    //		
    //		UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
    //		label.text = [NSString stringWithFormat:@"Slide %d\n", i + 1];
    //		label.backgroundColor = [UIColor blackColor];
    //		label.textColor = [UIColor whiteColor];
    //		[label sizeToFit];
    //		label.center = iv.center;
    //		[iv addSubview:label];
    //		[label release];
    //		
    //		[iv release];
    //	}
	
	cPage = currentPage;
	[self updatePagePlacement];
	
	[self.view addSubview:sv];
    
	pageControl.numberOfPages = NPAGES;
	pageControl.currentPage = currentPage;
	pageControl.enabled = NO;
}

- (void) updatePagePlacement
{
	CGPoint offset = sv.contentOffset;
    
	// Current Page
	UIView *v = [sv viewWithTag:900 + cPage];
	CGRect newframe = CGRectMake(offset.x, 0.0f, 320.0f, BASEHEIGHT);
	if (!CGRectEqualToRect(newframe, v.frame)) v.frame = newframe;
	// printf("Center: %d\n", cpage);
	
	// Pages to the left
	int half = (NPAGES / 2);
	float dx = -320.0f * half;
	for (int i = 0; i < half; i++)
	{
		int tag = (cPage + i + NPAGES - half) % NPAGES;
		// printf("Left: %d\n", tag);
		UIView *v = [sv viewWithTag:900 + tag];
		CGRect newframe = CGRectMake(offset.x + dx, 0.0f, 320.0f, BASEHEIGHT);
		if (!CGRectEqualToRect(newframe, v.frame)) v.frame = newframe;
		dx += 320.0f;
	}
	
	// Pages to the right
	int nleft = NPAGES - half;
	dx = 320.0f;
	for (int i = 1; i < nleft; i++)
	{
		int tag = (cPage + i + NPAGES) % NPAGES;
		// printf("Right: %d\n", tag);
		UIView *v = [sv viewWithTag:900 + tag];
		CGRect newframe = CGRectMake(offset.x + dx, 0.0f, 320.0f, BASEHEIGHT);
		if (!CGRectEqualToRect(newframe, v.frame)) v.frame = newframe;
		dx += 320.0f;
	}
}

-(void)viewShowTime:(id)sender{
//    MyCustomButton* myButton= (MyCustomButton*)sender;
//    Movie* mv = (Movie*)myButton.argument;
    ShowTimeInfo* showtimeView = [[ShowTimeInfo alloc]init];
    [self.navigationController pushViewController:showtimeView animated:YES];
}

-(void)viewTrailer:(id)sender{
    TrailerInfo* trailerView = [[TrailerInfo alloc]init];
    [self.navigationController pushViewController:trailerView animated:YES];
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
